layui.use(['element','layer','form','jquery','table'], function(){
  var layer = layui.layer
  ,element = layui.element
  ,form = layui.form
  ,$ = layui.jquery
  ,table = layui.table;
  
  var funtion_div = $('.function');
  var deaw_div = $('.draw');
  var exhibition = $('.exhibition');

  funtion_div.fadeIn(1,function(){
	  $(this).animate({
	  	height:'400px',
	  	width:'24%'
	  }); 
  });  
  deaw_div.fadeIn(1,function(){
	  $(this).animate({
	  	height:'400px',
	  	width:'24%'
	  }); 
  });  
  exhibition.fadeIn(1,function(){
	  $(this).animate({
	  	height:'400px',
	  	width:'24%'
	  }); 
	  $('.layui-table-view').slideDown();
  });


    /*随机数*/


  var arr_data,num,setInterval_num;
  var running = false;
  var ranking1 =  1;

    $.ajax({
      url:'/Index/index/sel_stu',
      success:function (result) {
        arr_data = eval('('+result+')');
      }
    });


  /*数组对象分割然后拼接*/
    Array.prototype.del=function(n) {
    　if(n<0){
        return this;
      }else{
        return this.slice(0,n).concat(this.slice(n+1,this.length));
      }
    } 

  $('#begin_end').click(function () {

    if ((arr_data == null) || (arr_data.length <1)) {
      layer.msg('数据不足！', {
        icon: 7,
        time: 2000 
      });
      $('#begin_end').attr('class','layui-btn layui-btn-normal layui-btn-disabled');
      $('#begin_end').attr('disabled');
      return false;
    }  else{
      if (running) {
            $('#begin_end').html('开始');
            running = false;
            arr_data=arr_data.del(num);
            clearInterval(setInterval_num);
            var article = $('article').html();
            //console.log(article);
            $.ajax({
              url:'/Index/index/begin_end',
              type:'post',
              data:{'article':article},
              success:function (result) {
                var data_arr = eval('('+result+')');
                var ranking =  ranking1++;
                var tbody = "<tr>";
                tbody += '<td>'+ranking+'号</td>';
                tbody += "<td>"+data_arr[0]['stuName']+"</td>";
                tbody += "<td>"+data_arr[0]['stuId']+"</td>";
                tbody += "<td>"+data_arr[0]['gender']+"</td>";
                tbody += "</tr>";
                $('.exhibition tbody').append(tbody);
              }
          });
        }else{
          $('#begin_end').html('停止');
          $('#even_smoke').remove();
          running = true;
          setInterval_num = setInterval(function () {
              num = Math.floor(Math.random() * arr_data.length + 1)-1;
              //console.log(arr_data[num]);
              $('article').html(arr_data[num]);
          },1);
        }
      return true;
    }



  });



  var pumping_num;
  var grouping = 1;
  /*连抽页面切换效果*/
  function effect() {
      $('#even_smoke').html('开始');
      $('#begin_end').remove();
       $(".exhibition").animate({
          top:'500px',
          opacity:'0',
        });
       $('.function').animate({
        left:'100px',
       });
       $('.function').animate({
        top:'150px',
        height:'200px',
       });
       $('.draw section article').remove();
       $('.draw section h1').remove();
       $('.draw').animate({
        top:'400px',
        height:'200px',
       });
       $('.draw').animate({
        left:'100px',
       });
       $('aside').fadeIn(2000);
       $('#even_smoke').animate({width:'100%'});
       $('.home').animate({display:'none',width:'100%'});
  }
  /*连抽*/

  $('#even_smoke').click(function () {
    if ((arr_data == null) || (arr_data.length <1)) {
      layer.msg('数据不足！', {
        icon: 7,
        time: 2000 
      });
      $('#even_smoke').attr('class','layui-btn layui-btn-normal layui-btn-disabled');
      $('#even_smoke').attr('disabled');
      return false;
    }else{
      if ($('#begin_end').length == 1) {

        effect();
        layer.prompt({
          title:'连抽',
          anim: 3,
          move: false,
          closeBtn: 0,
          content:'请输入连抽个数：<input type="number" class="layui-layer-input" value="">',
          btn:['确定',['关闭']],
          btn2:function (index, layero) {
            window.location.href="http://localhost/";
          }
        },function(value, index, elem){
              if (value == null) {
                console.log('value不能为空');
              }else if(value > (arr_data.length)){
                layer.msg('输入的值不能大于插入的数据个数',{
                  icon:7,
                  time:2000
                });
              }else if(value <= 1){
                layer.msg('输入的值不能小于等于1',{
                  icon:7,
                  time:2000
                });
              }else{
                pumping_num=value;
                //console.log(pumping_num);
                layer.close(index);
              }
          });
      }else{
        if (arr_data.length < 1) {
          layer.msg('没有数据了！', {
            icon: 7,
            time: 2000 
          });
          $('#even_smoke').attr('class','layui-btn layui-btn-normal layui-btn-disabled');
          $('#even_smoke').attr('disabled');
          return false;
        }else{
          start_operation();
          return true;
        }
      }

    }


  });


  /*开始操作*/

  function start_operation() {

    $('#even_smoke').html('下一组');
    running = true;
    var rand_data = [];
    for (var i = 0; i < pumping_num; i++) {
        num = Math.floor(Math.random() * arr_data.length + 1)-1; 
        rand_data[i] = arr_data[num];
        arr_data=arr_data.del(num);
        console.log(num);
        //console.log(arr_data);
    }
    //console.log(rand_data);
    $.ajax({
      url:'/Index/index/batch_sel',
      type:'post',
      data:{'rand_data':rand_data},
      success:function (result) {
        var data_arr = eval('('+result+')');
          var group = grouping++;
          var a = '<tr><td colspan="4" style="text-align:center;">第'+group+'组</td></tr>';
          $('aside tbody').append(a);
          for (var i = 0; i < data_arr.length; i++) {
            var ranking =  ranking1++;
            var tbody = "<tr>";
            tbody += '<td>'+ranking+'号</td>';
            tbody += "<td>"+data_arr[i]['stuName']+"</td>";
            tbody += "<td>"+data_arr[i]['stuId']+"</td>";
            tbody += "<td>"+data_arr[i]['gender']+"</td>";
            tbody += "</tr>";
            $('aside tbody').append(tbody);
          }
        
      }
    });

  }

});